package com.qike.duola.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.DuolaDeptService;
import com.qike.duola.service.DuolaGoodsService;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.DeptListVo;
import com.qike.duola.vo.DeptPullListVo;
import com.qike.duola.vo.GoodsListVo;
import com.qike.duola.vo.GoodsPullListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "商品控制器")
@CrossOrigin
public class GoodsController {

    private static Logger logger = LoggerFactory.getLogger(GoodsController.class);


    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private DuolaGoodsService duolaGoodsService;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加物料
     *
     * @return
     */
    @PostMapping("/goodsAdd")
    @ApiOperation("添加物料")
    @ResponseBody

    public BaseResult goodsAdd(@RequestBody GoodsAddDto goodsAddDto) {
        try {
            duolaGoodsService.GoodsAdd(goodsAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 编辑物料
     *
     * @return
     */
    @PostMapping("/goodsEdit")
    @ApiOperation("编辑物料")
    @ResponseBody
    public BaseResult goodsEdit(@RequestBody GoodsEditDto goodsEditDto) {
        try {
            duolaGoodsService.goodsEdit(goodsEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除物料
     *
     * @return
     */
    @PostMapping("/goodsDel")
    @ApiOperation("删除物料")
    @ResponseBody
    public BaseResult goodsDel(@RequestBody DeptDelDto deptDelDto) {
        try {
            duolaGoodsService.goodsDel(deptDelDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 物料列表
     *
     * @return
     */
    @GetMapping("/goodsList")
    @ApiOperation("物料列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = GoodsListVo.class)
    })
    public BaseResult goodsList(GoodsListDto goodsListDto) {
        try {
            GoodsListVo vo = duolaGoodsService.goodsList(goodsListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 通过物料类型获取物料列表不传获取所有
     *
     * @return
     */
    @ApiOperation("通过物料类型获取物料列表 不传获取所有")
    @GetMapping("/goodsPullList")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = GoodsPullListVo.class)
    })
    public BaseResult goodsPullList(Integer goodsType) {
        try {
            List<GoodsPullListVo> vo = duolaGoodsService.goodsPullList(goodsType);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 通过不选择的物料类型获取物料列表
     *
     * @return
     */
    @ApiOperation("通过不选择的物料类型获取物料列表")
    @GetMapping("/goodsPullListWithOut")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = GoodsPullListVo.class)
    })
    public BaseResult goodsPullListWithOut(Integer goodsType) {
        try {
            List<GoodsPullListVo> vo = duolaGoodsService.goodsPullListWithOut(goodsType);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
